package kassiopeia.agent.io.ping;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.common.base.Preconditions;
import kassiopeia.agent.infrastructure.AliveAgentsHolder;
import kassiopeia.agent.io.MessageReceiver;
import kassiopeia.commons.io.Message;
import kassiopeia.commons.io.Ping;

@Component("pingReceiver")
public class PingReceiver implements MessageReceiver {

    private AliveAgentsHolder agentsHolder;

    @Autowired
    public PingReceiver(AliveAgentsHolder agentsHolder) {
        Preconditions.checkNotNull(agentsHolder);
        this.agentsHolder = agentsHolder;
    }

    @Override
    public void onMessage(Message message) {
        Ping ping = (Ping) message;
        agentsHolder.ping(ping);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Class<? extends Message>[] acceptedMessages() {
        return new Class[] { Ping.class };
    }
}
